package sddz.foodmanage.adapter;

import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import sddz.foodmanage.R;
import sddz.foodmanage.model.CommodityBean;
import sddz.foodmanage.utils.ImageLoader;

/**
 * @Date:2019/8/10
 * @Author:lbb
 */

public class OrderSparseadpter extends BaseAdapter {
    private Context context;
    private SparseArray<CommodityBean.DataBeanX.DataBean> list;
    private ModifyCountInterface modifyCountInterface;
    private int userid;

    public OrderSparseadpter() {

    }

    public void setdata(Context context, SparseArray<CommodityBean.DataBeanX.DataBean> list, int userid) {
        this.context = context;
        this.list = list;
        this.userid = userid;
    }

    public OrderSparseadpter(Context context, SparseArray<CommodityBean.DataBeanX.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
        if (this.modifyCountInterface == null) {
            Log.e("modifyCountInterface", "为null");
        } else {
            Log.e("modifyCountInterface", "不为空");
        }
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        CommodityBean.DataBeanX.DataBean dataBean = (CommodityBean.DataBeanX.DataBean) getItem(position);
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.item_home_shangpin, null);
            holder = new ViewHolder();
            holder.spname = convertView.findViewById(R.id.tv_spname);
            holder.price = convertView.findViewById(R.id.tv_unitprice);
            holder.img = convertView.findViewById(R.id.image_sp);
            holder.dsx = convertView.findViewById(R.id.image_dsx);
            holder.weight = convertView.findViewById(R.id.tv_weight);
            holder.jia = convertView.findViewById(R.id.image_add);
            holder.jian = convertView.findViewById(R.id.image_jian);
            holder.num = convertView.findViewById(R.id.rl_num);
            holder.changesc = convertView.findViewById(R.id.rl_changesc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CommodityBean.DataBeanX.DataBean info=list.get(list.keyAt(position));
        if (info != null) {
            holder.spname.setText(info.getName());
            holder.price.setText(info.getPrice() + "元/" + info.getUnit());
            holder.weight.setText(info.getNum() + "");
            if (info.getImgs().size() > 0) {
                ImageLoader.loadImageUrl(context, info.getImgs().get(0).getUrl(), holder.img);
            }
            if (userid == 2) {
                holder.num.setVisibility(View.VISIBLE);//加减
                holder.changesc.setVisibility(View.GONE);//食材修改
            } else if (userid == 1) {
                holder.num.setVisibility(View.GONE);
                holder.changesc.setVisibility(View.VISIBLE);
            }
            if (info.getStatus() == 1) {
                holder.dsx.setVisibility(View.GONE);
            } else {
                holder.dsx.setVisibility(View.VISIBLE);
            }

            holder.jia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    modifyCountInterface.doIncrease(holder.weight, position);// 暴露增加接口
                }
            });


            holder.jian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease(holder.weight, position);// 暴露删减接口
                }
            });
            holder.weight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.doTextView(holder.weight, position);
                }
            });
            holder.changesc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.dochange(holder.changesc, position);
                }
            });
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.dodatu(holder.img, position);
                }
            });

        }

        return convertView;
    }


    class ViewHolder {
        ImageView img, dsx;
        ImageView jia, jian;
        TextView spname, price;
        TextView weight;
        AutoRelativeLayout num, changesc;
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doIncrease(View showCountView, int position);

        /**
         * 删减操作
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doDecrease(View showCountView, int position);

        public void doTextView(View showCountView, int position);

        public void dochange(View showCountView, int position);

        public void dodatu(View showCountView, int position);


    }
}
